#include "setwig.h"
#include "ui_setwig.h"
#include "globalval.h"
#define MQTT_sen "MQTT down"
QString styleSheet = "QPushButton#m_temp_add {"
                     "    border-radius: 50%;"
                     "    width: 100px;"
                     "    height: 100px;"
                     "}";

SetWig::SetWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWig)
{
    m_state = true;
    ui->setupUi(this);
    humi_threshold = 54;
    temp_threshold = 26;
    m_type = "1";
    temp1 = true;
    temp2 = true;
//    humi1 = true;
//    humi2 = true;
    Humidifier = false;

    ui->m_lcd1->display(temp_threshold);
    ui->m_lcd2->display(humi_threshold);
    QString styleSheet_temp = "QPushButton#m_temp_add {"
                              "    border-radius: 40px;"
//                              "    border-image: url(:/pic/add.png);"
                              "    width: 80px;"
                              "    height: 80px;"
                              "    background-color: #3498db; /* 按钮的背景颜色 */"
                              "    border: none; /* 移除边框 */"
                              "    color: green; /* 文字颜色 */"
                              "}"
                              "QPushButton#m_temp_add:hover {"
                              "    background-color: #2980b9; /* 鼠标悬停时的背景颜色 */"
                              "    box-shadow: 0 0 10px rgba(0,0,0,0.5); /* 鼠标悬停时的阴影效果 */"
                              "}"
                              "QPushButton#m_temp_add:pressed {"
                              "    background-color: #2c3e50; /* 按钮按下时的背景颜色 */"
                              "}"
                              "QPushButton#m_temp_reduce {"
                              "    border-radius: 40px;"
                              "    width: 80px;"
                              "    height: 80px;"
                              "    background-color: #3498db; /* 按钮的背景颜色 */"
                              "    border: none; /* 移除边框 */"
                              "    color: red; /* 文字颜色 */"
                              "}"
                              "QPushButton#m_temp_reduce:hover {"
                              "    background-color: #2980b9; /* 鼠标悬停时的背景颜色 */"
                              "    box-shadow: 0 0 10px rgba(0,0,0,0.5); /* 鼠标悬停时的阴影效果 */"
                              "}"
                              "QPushButton#m_temp_reduce:pressed {"
                              "    background-color: #2c3e50; /* 按钮按下时的背景颜色 */"
                              "}";
    QString styleSheet_humi = "QPushButton#m_humi_add {"
                              "    border-radius: 40px;"
                              "    width: 80px;"
                              "    height: 80px;"
                              "    background-color: #3498db; /* 按钮的背景颜色 */"
                              "    border: none; /* 移除边框 */"
                              "    color: green; /* 文字颜色 */"
                              "}"
                              "QPushButton#m_humi_add:hover {"
                              "    background-color: #2980b9; /* 鼠标悬停时的背景颜色 */"
                              "    box-shadow: 0 0 10px rgba(0,0,0,0.5); /* 鼠标悬停时的阴影效果 */"
                              "}"
                              "QPushButton#m_humi_add:pressed {"
                              "    background-color: #2c3e50; /* 按钮按下时的背景颜色 */"
                              "}"
                              "QPushButton#m_humi_reduce {"
                              "    border-radius: 40px;"
                              "    width: 80px;"
                              "    height: 80px;"
                              "    background-color: #3498db; /* 按钮的背景颜色 */"
                              "    border: none; /* 移除边框 */"
                              "    color: red; /* 文字颜色 */"
                              "}"
                              "QPushButton#m_humi_reduce:hover {"
                              "    background-color: #2980b9; /* 鼠标悬停时的背景颜色 */"
                              "    box-shadow: 0 0 10px rgba(0,0,0,0.5); /* 鼠标悬停时的阴影效果 */"
                              "}"
                              "QPushButton#m_humi_reduce:pressed {"
                              "    background-color: #2c3e50; /* 按钮按下时的背景颜色 */"
                              "}";

    ui->m_temp_add->setFixedSize(80, 80);
    ui->m_temp_add->setStyleSheet(styleSheet_temp);

    ui->m_temp_reduce->setStyleSheet(styleSheet_temp);
    ui->m_temp_reduce->setFixedSize(80, 80);

    ui->m_humi_add->setStyleSheet(styleSheet_humi);
    ui->m_humi_add->setFixedSize(80,80);

    ui->m_humi_reduce->setStyleSheet(styleSheet_humi);
    ui->m_humi_reduce->setFixedSize(80, 80);

    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&SetWig::checkSlots);
    timer->start(1000);

   // ui->m_lcd1_1->display(25);
}


SetWig::~SetWig()
{
    delete ui;
}

void SetWig::on_pushButton_clicked()
{
    QJsonObject obj;

    obj["command"] = "REFRESH";
    obj["LED_Status"] = m_state;
    obj["type"] = m_type;

    QJsonDocument doc(obj);
    QByteArray jsonData = doc.toJson();

    QMQTT::Message msg;
    msg.setTopic(MQTT_sen);
    msg.setPayload(jsonData);

   GlobalVal::mqtt->publish(msg);
   qDebug() << jsonData;
}

void SetWig::on_comboBox_activated(int index)
{ 
    if(index == 3)
    {
         m_type = QString::number(0);
    }else
    {
        m_type = QString::number(index+1);
    }

    qDebug() <<m_type;
}

void SetWig::on_pushButton_2_clicked()
{
    if(m_state == true)
    {
        m_state = false;
    }
    else
    {
        m_state = true;
    }
    light_show(m_state);

}


void SetWig::dataUpSlots1(const QByteArray &msg)
{
    QJsonDocument doc = QJsonDocument::fromJson(msg);

    if(!doc.isNull())
    {
        QJsonObject root = doc.object();
        double lcd1 = root["temp"].toDouble();
        ui->m_lcd1_1 ->display(lcd1);

        int lcd2 = root["humi"].toInt();
        ui->m_lcd2_2->display(lcd2);

        qDebug() << "temp: " << lcd1 << "humi: " << lcd2;
    }
}

void SetWig::checkSlots()
{
    static int num = 0;

    double value1 = ui->m_lcd1_1->value();
    int value2 = ui->m_lcd1->value();
//    int value3 = ui->m_lcd2->value();
//    int value4 = ui->m_lcd2_2->value();
    if(value1 > value2)
    {
        if(temp1 == true)
        {
           num = 1;
        }
        temp1 = false;
    }
    if(value1 < value2)
    {
        if(temp2 == true)
        {
           num = 2;
        }
        temp2 = false;
    }
//    if(value3 > value4)
//    {
//        if(humi1 == true)
//        {
//           num = 3;
//        }
//        humi1 = false;
//    }
//    if(value3 < value4)
//    {
//        if(humi2 == true)
//        {
//           num = 2;
//        }
//        humi2 = false;
//    }
    if(num == 1)
    {
        QJsonObject obj;

        obj["LED_Status"] = true;

        QJsonDocument doc(obj);
        QByteArray jsonData = doc.toJson();

        QMQTT::Message msg;
        msg.setTopic(MQTT_sen);
        msg.setPayload(jsonData);

        GlobalVal::mqtt->publish(msg);
        qDebug() << jsonData;
        num = 0;
        temp2 = true;
    }
    else if(num == 2)
    {
        QJsonObject obj;

        obj["LED_Status"] = false;

        QJsonDocument doc(obj);
        QByteArray jsonData = doc.toJson();

        QMQTT::Message msg;
        msg.setTopic(MQTT_sen);
        msg.setPayload(jsonData);

        GlobalVal::mqtt->publish(msg);
        qDebug() << jsonData;
        num = 0;
        temp1 = true;
    }
    else
    {

    }
}

void SetWig::light_show(bool data)
{
    if(data == true)
    {
        ui->pushButton_2->setText("ON");
    }
    else
    {
        ui->pushButton_2->setText("OFF");
    }
}

void SetWig::on_m_temp_add_clicked()
{
    temp_threshold++;
    ui->m_lcd1->display(temp_threshold);
}

void SetWig::on_m_temp_reduce_clicked()
{
    temp_threshold--;
    ui->m_lcd1->display(temp_threshold);
}

void SetWig::on_m_humi_add_clicked()
{
    humi_threshold++;
    ui->m_lcd2->display(humi_threshold);
}

void SetWig::on_m_humi_reduce_clicked()
{
    humi_threshold--;
    ui->m_lcd2->display(humi_threshold);
}

//    QJsonDocument doc = QJsonDocument::fromJson(msg);
//    QJsonObject root = doc.object();

//    if(root.contains("data")&&root["data"].isArray())
//    {
//        QJsonArray dataArray = root["data"].toArray();
//        for(int i = 0;i < dataArray.size();i++)
//        {
//            QJsonValue dataValue = dataArray.at(i);
//            if(dataValue.isObject()){
//                QJsonObject dataObj = dataValue.toObject();
//                int key = dataObj["key"].toInt();
//                QString name = dataObj["name"].toString();
//                int data = dataObj["data"].toInt();
//                qDebug() << "SetWig:" << "Key:" << key << " Name:" << name << " Data:" << data;
//                if(key == 1)
//                {
//                    int lcd1 = dataObj["data"].toInt();
//                    ui->m_lcd1_1 ->display(lcd1);
//                }
//                if(key == 2)
//                {
//                    int lcd2 = dataObj["data"].toInt();
//                    ui->m_lcd2_2->display(lcd2);
//                }
//            }
//        }
//    }




//    QJsonObject threshold1;
//    threshold1["name"] = "temp_threshold";
//    threshold1["data"] = temp_threshold;
//    QJsonObject threshold2;
//    threshold2["name"] = "humi_threshold";
//    threshold2["data"] = humi_threshold;
//    QJsonArray dataArray = {threshold1,threshold2};
//    obj["threshold"] = dataArray;
//    QJsonObject type;
//    type["type"] = m_type;
//    QJsonObject stm32;
//    stm32["key"] = 3;
//    stm32["name"] = "light";
//    stm32["data"] = m_state;


